import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/config/Config.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/model/OrderModel.dart';
import 'package:flutter_yishop/services/SignService.dart';
import 'package:flutter_yishop/services/UserService.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';

class OrderPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _OrderPageState();
  }

}

class _OrderPageState extends State<OrderPage>{

  List _orderList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getListData();
  }

  void _getListData() async {
    List userinfo =  UserService.getUserInfo();

    var tempJson = {"uid": userinfo[0]['_id'], "salt": userinfo[0]["salt"]};

    var sign = SignService.getSign(tempJson);

    var api =
        '${Config.url}api/orderList?uid=${userinfo[0]['_id']}&sign=${sign}';

    var response = await Dio().get(api);
    print(response.data is Map);

    setState(() {
      var orderMode = new OrderModel.fromJson(response.data);

      this._orderList = orderMode.result;

      print(this._orderList[0].name);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("订单"),
      ),
      body:  Stack(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(0, ScreenAdapter.height(80), 0, 0),
            padding: EdgeInsets.all(ScreenAdapter.width(16)),
            child:this._orderList.length>0? ListView(
              children: this._orderList.map((value){
                return _orderItemWidget(value);
              }).toList()
            ):Container(
              child: Center(
                child: Text("暂无订单"),
              ),
            ),
          ),
          Positioned(
            top: 0,
            width: ScreenAdapter.width(750),
            height: ScreenAdapter.height(76),
            child: Container(
              width: ScreenAdapter.width(750),
              height: ScreenAdapter.height(76),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Text("全部", textAlign: TextAlign.center),
                  ),
                  Expanded(
                    child: Text("待付款", textAlign: TextAlign.center),
                  ),
                  Expanded(
                    child: Text("待收货", textAlign: TextAlign.center),
                  ),
                  Expanded(
                    child: Text("已完成", textAlign: TextAlign.center),
                  ),
                  Expanded(
                    child: Text("已取消", textAlign: TextAlign.center),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _orderItemWidget(value) {
    return GestureDetector(
      child: Card(
        child: Column(
          children: <Widget>[
            ListTile(
              title: Text("订单编号：${value.sId}"),
            ),
            Divider(),
            Column(
              children: this._orderItemListWidget(value.orderItem),
            ),
            SizedBox(height: 10),
            ListTile(
              leading: Text("合计：￥${value.allPrice}"),
              trailing: FlatButton(
                child: Text("申请售后"),
                onPressed: () {},
                color: Colors.grey[100],
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        Navigator.pushNamed(context, Constants.ORDERINFO,
            arguments: {
              "result":value
            });
      },
    );


  }


  List<Widget> _orderItemListWidget(orderItems) {
    List<Widget> tempList = [];
    for (var i = 0; i < orderItems.length; i++) {
      tempList.add(Column(
        children: <Widget>[
          SizedBox(height: 10),
          ListTile(
            leading: Container(
              width: ScreenAdapter.width(120),
              height: ScreenAdapter.height(120),
              child: Image.network(
                '${orderItems[i].productImg}',
                fit: BoxFit.cover,
              ),
            ),
            title: Text("${orderItems[i].productTitle}"),
            trailing: Text('x${orderItems[i].productCount}'),
          ),
          SizedBox(height: 10)
        ],
      ));
    }
    return tempList;
  }
}